<template>
  <div class="order-details-container">
    <!-- 返回按钮 -->
    <el-button type="text" @click="goBack" class="back-btn">
      <el-icon><ArrowLeft /></el-icon>
      <span>返回</span>
    </el-button>

    <!-- 订单内容 -->
    <div v-if="order" class="order-content">
      <!-- 订单状态 -->
      <div class="status-section">
        <el-tag :type="getStatusTagType(order.status)" class="status-tag">
          {{ getStatusText(order.status) }}
        </el-tag>
      </div>

      <!-- 订单基本信息 -->
      <div class="info-section">
        <h3 class="section-title">订单信息</h3>
        <div class="info-grid">
          <div class="info-item">
            <span class="info-label">订单编号：</span>
            <span class="info-value">{{ order.orderNo }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">下单时间：</span>
            <span class="info-value">{{ formatDate(order.createTime) }}</span>
          </div>
        </div>
      </div>

      <!-- 收货地址信息 -->
      <div class="info-section">
        <h3 class="section-title">收货信息</h3>
        <div v-if="order.address" class="address-info">
          <div class="recipient-info">
            <span>{{ order.address.receiver }} {{ order.address.phone }}</span>
          </div>
          <div class="address-detail">
            {{ order.address.province }}{{ order.address.city }}{{ order.address.district }}{{ order.address.detail }}
          </div>
        </div>
      </div>

      <!-- 商品信息 -->
      <div class="info-section" v-if="order.orderItems">
        <h3 class="section-title">商品信息</h3>
        <div class="goods-list">
          <div v-for="(item, index) in order.orderItems" :key="index" class="goods-item">
            <el-image :src="item.imageUrl" :preview-src-list="[item.imageUrl]" class="goods-image"></el-image>
            <div class="goods-info">
              <div class="goods-name">{{ item.productName }}</div>
              <div class="goods-spec">{{ item.specifications }}</div>
              <div class="goods-price-count">
                <span class="goods-price">¥{{ item.price }}</span>
                <span class="goods-count">x{{ item.quantity }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 订单金额 -->
      <div class="info-section">
        <h3 class="section-title">订单金额</h3>
        <div class="amount-info">
          <div class="amount-item">
            <span class="amount-label">商品总价：</span>
            <span class="amount-value">¥{{ order.productTotal || 0 }}</span>
          </div>
          <div class="amount-item">
            <span class="amount-label">运费：</span>
            <span class="amount-value">¥{{ order.shippingFee || 0 }}</span>
          </div>
          <div class="amount-item total">
            <span class="amount-label">实付金额：</span>
            <span class="amount-value total-price">¥{{ order.totalAmount || 0 }}</span>
          </div>
        </div>
      </div>

      <!-- 底部操作按钮 -->
      <div class="bottom-actions">
        <el-button v-if="order.status === 'WAITING_PAYMENT'" @click="handlePayOrder" type="primary">
          立即支付
        </el-button>
        <el-button v-if="order.status === 'WAITING_PAYMENT'" @click="handleCancelOrder" type="text">
          取消订单
        </el-button>
        <el-button v-if="order.status === 'WAITING_RECEIVE'" @click="handleConfirmOrder" type="primary">
          确认收货
        </el-button>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-else class="loading">
      <el-empty description="订单数据加载中..." />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { findOrderById } from '@/api/order.js'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const order = ref(null)

// 获取订单ID
const orderId = route.params.id

// 获取订单详情
const fetchOrderDetails = async () => {
  try {
    const response = await findOrderById(orderId)
    if (response.code === 200) {
      order.value = response.data
    } else {
      ElMessage.error('获取订单详情失败')
    }
  } catch (error) {
    console.error('获取订单详情出错:', error)
    ElMessage.error('获取订单详情失败')
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 获取订单状态文本
const getStatusText = (status) => {
  const statusMap = {
    'WAITING_PAYMENT': '待付款',
    'WAITING_SHIPMENT': '待发货',
    'WAITING_RECEIVE': '待收货',
    'COMPLETED': '已完成',
    'CANCELLED': '已取消'
  }
  return statusMap[status] || status
}

// 获取订单状态标签类型
const getStatusTagType = (status) => {
  const typeMap = {
    'WAITING_PAYMENT': 'warning',
    'WAITING_SHIPMENT': 'primary',
    'WAITING_RECEIVE': 'info',
    'COMPLETED': 'success',
    'CANCELLED': 'danger'
  }
  return typeMap[status] || 'default'
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 订单操作方法
const handlePayOrder = () => {
  ElMessage.success('跳转到支付页面')
}

const handleCancelOrder = async () => {
  try {
    ElMessage.success('订单已取消')
    await fetchOrderDetails()
  } catch (error) {
    ElMessage.error('取消订单失败')
  }
}

const handleConfirmOrder = async () => {
  try {
    ElMessage.success('已确认收货')
    await fetchOrderDetails()
  } catch (error) {
    ElMessage.error('确认收货失败')
  }
}

// 页面加载时获取订单详情
onMounted(() => {
  fetchOrderDetails()
})
</script>

<style scoped>
.order-details-container {
  padding: 10px 16px 80px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.back-btn {
  margin-bottom: 16px;
  font-size: 18px;
  color: #ff6700; /* 橙色主题色 */
}

.order-content {
  width: 100%;
}

.status-section {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 12px;
  text-align: center;
}

.status-tag {
  font-size: 16px;
  padding: 6px 16px;
}

.info-section {
  background-color: #fff;
  padding: 16px;
  border-radius: 8px;
  margin-bottom: 12px;
}

.section-title {
  margin: 0 0 12px 0;
  font-size: 14px;
  font-weight: 500;
  color: #303133;
}

.info-grid, .address-info, .amount-info {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.info-item, .amount-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.info-label, .amount-label {
  color: #606266;
}

.info-value, .amount-value {
  color: #303133;
}

.recipient-info {
  margin-bottom: 5px;
}

.address-detail {
  color: #303133;
  line-height: 1.5;
  word-break: break-word;
}

.goods-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.goods-item {
  display: flex;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.goods-item:last-child {
  border-bottom: none;
}

.goods-image {
  width: 80px;
  height: 80px;
  border-radius: 4px;
  object-fit: contain;
  background-color: #f5f5f5;
}

.goods-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.goods-name {
  font-weight: 500;
  margin-bottom: 5px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 14px;
}

.goods-spec {
  color: #909399;
  font-size: 12px;
  margin-bottom: 5px;
}

.goods-price-count {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.goods-price {
  color: #e6a23c;
  font-weight: 500;
  font-size: 14px;
}

.amount-item.total {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid #f0f0f0;
}

.total-price {
  color: #e6a23c;
  font-size: 16px;
  font-weight: 500;
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 20px;
  box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.05);
  gap: 12px;
}

.loading {
  padding: 60px 0;
  text-align: center;
}
</style>